package com.ny.zmb.openapi.ysutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ny.zmb.openapi.entity.PmApiLog;
import com.ny.zmb.openapi.service.IPmApiLogService;
import com.ny.zmb.openapi.ysutil.base.BaseBizReqCommand;
import com.ny.zmb.openapi.ysutil.base.BaseHeadReqCommand;
import com.ny.zmb.openapi.ysutil.base.HeadResponseCommand;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.utils.*;
import lombok.Data;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description 客户端
 * @Author linxl
 * @Date 2021/4/1 9:18
 * @Version 1.0
 */
@Data
public class YinShengClient {

    /**
     * 接口名称
     */
    private String api;
    /**
     * 环境
     */
    private String env;

    private String url;
    /**
     * 证书路径
     */
    private String privateCerPath;

    /**
     * 私钥证书密码
     */
    private String privateCerPwd;

    private ServiceEnum serviceEnum;

    /**
     * 交易
     *
     * @param bizReq bizReqJson 业务参数
     * @return
     */
    public JSONObject doAction(BaseHeadReqCommand baseReqCommand, BaseBizReqCommand bizReq) {

        try {
            ValidateUtils.validate(bizReq);
            return requestApi(baseReqCommand, JsonParser.toJson(bizReq));
        } catch (Exception e) {
            YinshengLogger.logBizError(e);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * <p>基本请求</p>
     *
     * @user zcw 2021年6月21日 下午5:25:07
     */
    public JSONObject baseAction(BaseHeadReqCommand baseReqCommand, Object bizReq) {
        try {
            ValidateUtils.validate(bizReq);
            return requestApi(baseReqCommand, JsonParser.toJson(bizReq));
        } catch (Exception e) {
            YinshengLogger.logBizError(e);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * <p>接口请求</p>
     *
     * @user zcw 2021年6月21日 下午5:24:41
     */
    public JSONObject requestApi(BaseHeadReqCommand baseReqCommand, String bizReq) {
        try {
            ValidateUtils.validate(baseReqCommand);
            baseReqCommand.setBizReqJson(bizReq);
            Map<String, String> buildResponseSign = YsMpPayUtils.buildResponseSign(privateCerPwd, privateCerPath,
                    baseReqCommand);
            String result = HttpClient.post(url, buildResponseSign);
            if (StringUtils.isBlank(result)) {
                YinshengLogger.logBizInfo("银盛渠道返回数据为空");
                throw new YsChannelClientException(YsChannelEnum.CHANNEL_NULL_MSG_FAIL.getCode(),
                        YsChannelEnum.CHANNEL_NULL_MSG_FAIL.getMessage());
            }
            return JSONObject.parseObject(result);
        } catch (Exception e) {
            YinshengLogger.logBizInfo("银盛渠道返回数据sdk解析出错");
            YinshengLogger.logBizError(e);
            throw new RuntimeException(e.getMessage());
        }
    }

    // 统一异常处理
    public HeadResponseCommand exceptionHandle(Exception e) {
        if (e instanceof YsChannelClientException) {
            YsChannelClientException ysChannelClientException = (YsChannelClientException) e;
            return new HeadResponseCommand(ysChannelClientException.getCode(), ysChannelClientException.getMessage());
        }
        return new HeadResponseCommand(YsChannelEnum.REQ_IO_EXCEPTION_2_FAIL.getCode(),
                YsChannelEnum.REQ_IO_EXCEPTION_2_FAIL.getMessage());

    }

    public YinShengClient(ServiceEnum serviceEnum, String env, String privateCerPath, String privateCerPwd) {
        this.serviceEnum = serviceEnum;
        this.api = serviceEnum.getApiName();
        this.privateCerPath = privateCerPath;
        this.privateCerPwd = privateCerPwd;
        this.env = env;
        this.url = serviceEnum.getTestUrl();
        if (YsPayConstant.ENV_PRD.equals(env)) {
            this.url = serviceEnum.getPrdUrl();
        }
    }
}
